package com.fh.fruit.controller.bill;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.fruit.service.bill.BillManager;
import com.fh.fruit.util.DateUtil;
import com.fh.util.Jurisdiction;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "bill")
public class BillController extends BaseController {
	String menuUrl = "bill/list.do"; //菜单地址(权限用)
	
    @Resource(name = "billManager")
    private BillManager billManager;


    /**账单列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView listGoods(Page page,HttpSession httpSession)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		String num=pd.getString("num");
		if(num != null){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Date date = sdf.parse(DateUtil.getTime());
			Date date1=DateUtil.getNextDay(date);
			pd.put("time",sdf.format(date1));
		}
		page.setShowCount(15);
		page.setPd(pd);
		List<Map<String,Object>> billList = billManager.listBill(page);
		BigDecimal enter = new BigDecimal("0.00"); 
		BigDecimal out = new BigDecimal("0"); 
		for (int i = 0; i < billList.size(); i++) {
			String price_state=(String)billList.get(i).get("price_state");
			String state=(String)billList.get(i).get("state");
			if("01".equals(state)||"03".equals(state)){
				if(price_state!=null){
					BigDecimal price= (BigDecimal)billList.get(i).get("total_price");
					if("01".equals(price_state)){
						out=out.add(price);
					}else{
						enter=enter.add(price);
					}
				}
			}
		}
		pd.put("enter", enter);
		pd.put("out", out);
		mv.setViewName("farm/bill/bill_list");
		mv.addObject("billList", billList);
		mv.addObject("pd", pd);
		mv.addObject("QX",Jurisdiction.getHC());	//按钮权限
		return mv;
	}
	
	
	/**导出账单信息到EXCEL
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = this.getPageData();
		try{
			if(Jurisdiction.buttonJurisdiction(menuUrl, "cha")){
				
				Map<String,Object> dataMap = new HashMap<String,Object>();
				List<String> titles = new ArrayList<String>();
				titles.add("排序"); 		//1
				titles.add("ID");  		//2
				titles.add("支付编号");			//3
				titles.add("手机号");			//4
				titles.add("账单金额");			//5
				titles.add("支付金额");			//6
				titles.add("付款方式");		//7
				titles.add("金额状态");	//8
				titles.add("账单描述");//9
				titles.add("支付状态");//10
				titles.add("支付类型");//11
				titles.add("创建时间");//12
				titles.add("支付时间");//13
				dataMap.put("titles", titles);
				page.setShowCount(15);
				page.setCurrentPage(Integer.parseInt(pd.getString("pageNum")));
				page.setPd(pd);
				List<Map<String,Object>> billList = billManager.listBill(page);
				List<PageData> varList = new ArrayList<PageData>();
				for(int i=0;i<billList.size();i++){
					PageData vpd = new PageData();
					String pay_mode=(String)billList.get(i).get("pay_mode");
					String price_state=(String)billList.get(i).get("price_state");
					String state=(String)billList.get(i).get("state");
					String type=(String)billList.get(i).get("type");
					String finish_time=(String)billList.get(i).get("finish_time");
					if(pay_mode != null && !"".equals(pay_mode)){
						if("wechat".equals(pay_mode)){
							pay_mode="微信";
						}else{
							pay_mode="支付宝";
						}
					}else{
						pay_mode="非消费";
					}
					if(price_state != null && !"".equals(price_state)) {
						if("01".equals(price_state)){
							price_state="支出";
						}else{
							price_state="收入";
						}
					}
					if(state != null && !"".equals(state)){
						if("00".equals(state)){
							state="未支付";
						}else if("01".equals(state)){
							state="已支付";
						}else if("02".equals(state)){
							state="已取消";
						}else if("03".equals(state)){
							state="提现完成";
						}
					}
					if(type != null && !"".equals(type)){
						if("00".equals(type)){
							type="消费";
						}else if("01".equals(type)){
							type="充值";
						}else if("02".equals(type)){
							type="返销";
						}else if("03".equals(type)){
							type="提现";
						}else if("04".equals(type)){
							type="推荐支出";
						}
					}
					if(finish_time==null || "".equals(finish_time)){
						finish_time="未支付";
					}
					vpd.put("var1", (i+1));		//1
					vpd.put("var2", billList.get(i).get("id").toString());		//2
					vpd.put("var3", (String)billList.get(i).get("code"));			//3
					vpd.put("var4", (String)billList.get(i).get("phone"));	//4
					vpd.put("var5", billList.get(i).get("total_price").toString());		//5
					vpd.put("var6", billList.get(i).get("pay_price").toString());		//6
					vpd.put("var7", pay_mode);	//7
					vpd.put("var8", price_state);			//8
					vpd.put("var9", (String)billList.get(i).get("remark"));			//9
					vpd.put("var10", state);			//10
					vpd.put("var11", type);			//11
					vpd.put("var12", (String)billList.get(i).get("create_time"));			//12
					vpd.put("var13", finish_time);			//13
					varList.add(vpd);
				}
				dataMap.put("varList", varList);
				ObjectExcelView erv = new ObjectExcelView();					//执行excel操作
				mv = new ModelAndView(erv,dataMap);
			}
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
}
